package com.muh.controller;

import com.alibaba.fastjson.JSONArray;
import com.muh.entity.Employee;
import com.muh.operation.AggEmployeeOperationService;
import com.muh.operation.EmployeeOperationService;
import com.muh.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    EmployeeOperationService employeeOperationService;

    @Autowired
    AggEmployeeOperationService aggEmployeeOperationService;

    @PostMapping("addDocument")
    public Employee addDocument(@RequestBody Employee employee) {
        return employeeService.save(employee);
    }

    @PostMapping("addDocuments")
    public Object addDocuments(@RequestBody List<Employee> employees) {
        return employeeService.saveAll(employees);
    }

    @PostMapping("editDocument")
    public Employee editDocument(@RequestBody Employee employee) {
        return employeeService.update(employee);
    }

    @GetMapping("findById")
    public Employee findById(Long id) {
        return employeeService.findById(id);
    }

    @GetMapping("findAll")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @GetMapping("findAllPage")
    public Page<Employee> findAllPage(@RequestParam(defaultValue = "0") int pageNumber,
                                      @RequestParam(defaultValue = "10") int pageSize) {
        Page<Employee> all = employeeService.findAllPage(pageNumber, pageSize);
        return all;
    }

    @GetMapping("findPageByAddress")
    public Page<Employee> findPageByAddress(@RequestParam String address,
                                            @RequestParam(defaultValue = "0") int pageNumber,
                                            @RequestParam(defaultValue = "10") int pageSize) {
        Page<Employee> pageByAddress = employeeService.findPageByAddress(address, pageNumber, pageSize);
        return pageByAddress;
    }

    @GetMapping("findPageHighlightByAddress")
    public Page<Employee> findPageHighlightByAddress(@RequestParam String address,
                                            @RequestParam(defaultValue = "0") int pageNumber,
                                            @RequestParam(defaultValue = "10") int pageSize) {
        Page<Employee> pageByAddress = employeeOperationService.findPageHighlightByAddress(address, pageNumber, pageSize);
        return pageByAddress;
    }

    @GetMapping("stats")
    public Map<String, Object> stats(@RequestParam String address) {
        Map<String, Object> result = employeeOperationService.getEmployeeStatistics(address);
        return result;
    }

    @GetMapping("metricAggregation")
    public Map<String, Object> statsd(@RequestParam String address) {
        Map<String, Object> result = aggEmployeeOperationService.metricAggregation(address);
        return result;
    }

    @GetMapping("bucketAggregation")
    public JSONArray bucketAggregation(@RequestParam String address) {
        JSONArray result = aggEmployeeOperationService.bucketAggregation(address);
        return result;
    }

    @GetMapping("findEmployeesByAddress")
    public List<Employee> findEmployeesByAddress(@RequestParam("address") String address) {
        return employeeService.findEmployeesByAddress(address);
    }

    @PostMapping("deleteDocumentById")
    public boolean deleteDocumentById(Long id) {
        boolean b = employeeService.deleteDocumentById(id);
        return b;
    }

    @PostMapping("deleteDocumentAll")
    public String deleteDocumentAll() {
        employeeService.deleteAll();
        return "success";
    }
}